/**
 * @param {number[]} nums
 * @return {number}
*/
var majorityElement = function(nums) {
  let result = nums[0], count = 1;
  for(let i = 1; i < nums.length; i++) {
    if(nums[i] != result) {
      if(count == 0) {
        count++;
        result = nums[i];
      }
      count--;
    } else {
      count++;
    }
  }
  return result;
};
let nums = [1, 2, 3, 2, 2, 2, 5, 4, 2];
console.log(majorityElement(nums));
